package models;

import java.util.*;
import javax.persistence.*;

import play.Logger;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

/**
 * User entity managed by Ebean
 */
@Entity
@Table(name="v_MovieSessionStartEnd")
public class View_MovieSessionStartEnd extends Model {

	@Id
	public Long id;

	public int house_id;

	public Date showtime;	

	public Date showtime_end;
	



    // -- Queries
    public static Model.Finder<Long,View_MovieSessionStartEnd> find = new Model.Finder(Long.class, View_MovieSessionStartEnd.class);	
	
	public static boolean isTimeSlotOccupied(int house_id, Date start, Date end){
		boolean startCollied = 
			(find.where().eq("house_id", house_id).eq("showtime",start).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).eq("showtime",end).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).between("showtime",start,end).findRowCount() > 0);
			
		boolean endCollied = 
			(find.where().eq("house_id", house_id).eq("showtime_end",start).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).eq("showtime_end",end).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).between("showtime_end",start,end).findRowCount() > 0);
		
		return (startCollied || endCollied);
	}	
	
	public static boolean isTimeSlotOccupied(Long currentId, int house_id, Date start, Date end){
		boolean startCollied = 
			(find.where().eq("house_id", house_id).ne("id",currentId).eq("showtime",start).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).ne("id",currentId).eq("showtime",end).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).ne("id",currentId).between("showtime",start,end).findRowCount() > 0);
			
		boolean endCollied = 
			(find.where().eq("house_id", house_id).ne("id",currentId).eq("showtime_end",start).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).ne("id",currentId).eq("showtime_end",end).findRowCount() > 0) ||
			(find.where().eq("house_id", house_id).ne("id",currentId).between("showtime_end",start,end).findRowCount() > 0);
		
		return (startCollied || endCollied);
	}

}

